﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace Weight.Base
{
    [XmlTypeAttribute(AnonymousType = true)]
    [XmlRootAttribute(Namespace = "", IsNullable = false)]
    public class PhraseConfigEntity:BaseSetting
    {
        /// <summary>
        /// 词组字典
        /// </summary>
        public List<PhraseMap> PhraseMaps;

        /// <summary>
        /// 称重台号
        /// </summary>
        public List<ScaleNum> Scales;

        /// <summary>
        /// 当前配置版本号
        /// </summary>
        [XmlIgnore]
        public int CurrentVersion = 2;

        /// <summary>
        /// 数据类型
        /// </summary>
        [XmlArray]
        public List<string> DataType { get; set; }

        /// <summary>
        ///仪表类型
        /// </summary>
        public List<MeterType> MeterTypes { get; set; }

        /// <summary>
        /// 波特率集合
        /// </summary>
        [XmlArray]
        public List<string> Buderates { get; set; }

        /// <summary>
        /// 计量单位集合
        /// </summary>
        [XmlArray]
        public List<WeightUnit> WeightUnits { get; set; }

        /// <summary>
        /// 控制器设备名称集合
        /// </summary>
        [XmlArray]
        public List<string> ControlDevices { get; set; }

        /// <summary>
        /// 控制器端子
        /// </summary>
        [XmlArray]
        public List<int> ControlPorts { get; set; }

        /// <summary>
        /// 省份简称
        /// </summary>
        public List<Province> Provices { get; set; }

        /// <summary>
        /// 字母简称
        /// </summary>
        public List<Letter> Letters { get; set; }


        public List<Expression> Expressions { get; set; }

        public List<RelationLetter> Relations { get; set; }


        public List<LogicalLetter> Logicals { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public   PhraseConfigEntity()
        {
            PhraseMaps = new List<PhraseMap>();
            DataType = new List<string>();
            Scales = new List<ScaleNum>();
            MeterTypes = new List<MeterType>();
            Buderates = new List<string>();
            WeightUnits = new List<WeightUnit>();
            ControlDevices = new List<string>();
            ControlPorts = new List<int>();
            Provices = new List<Province>();
            Letters = new List<Letter>();
            Expressions = new List<Expression>();
            Relations = new List<RelationLetter>();
            Logicals = new List<LogicalLetter>();
        }
        /// <summary>
        /// 保存默认配置
        /// </summary>
        public override void setDefaultConfig()
        {
            //设置默认词组
            if (PhraseMaps.Count == 0)
            {
                for (int index = 0; index < 10; index++)
                {
                    PhraseMaps.Add(new PhraseMap());
                }
            }

            //设置默认台号
            if(Scales.Count == 0)
            {
                for(int index=0; index < 10;  index++)
                {
                    Scales.Add(new ScaleNum() {  Code="0"+index, Name="台号(0"+index+")" });
                }
            }

            if(DataType.Count == 0)
            {
                //设置默认数据类型
                DataType.Add("字符串");
                DataType.Add("数字");
                DataType.Add("整数");
                DataType.Add("日期");
            }
      
            if(MeterTypes.Count == 0)
            {
                for (int i = 0; i < 5; i++)
                {
                    MeterTypes.Add(new MeterType()
                    {
                        Name = "待配置",
                        Code = "待配置"
                    });
                }
            }
           
            if(Buderates.Count == 0)
            {
                //波特率
                Buderates.Add("100");
                Buderates.Add("300");
                Buderates.Add("1200");
                Buderates.Add("2400");
                Buderates.Add("4800");
                Buderates.Add("9600");
                Buderates.Add("19200");
                Buderates.Add("38400");
            }
       

            if(WeightUnits.Count == 0)
            {
                //计量单位初始化
                WeightUnits.Add(new WeightUnit()
                {
                    Name = "公斤",
                    Code = "KG"
                });

                WeightUnits.Add(new WeightUnit()
                {
                    Name = "吨",
                    Code = "T"
                });
            }
            
            ///控制器设备名称
            if(ControlDevices.Count == 0)
            {
                ControlDevices.Add("道闸1");
                ControlDevices.Add("道闸2");
                ControlDevices.Add("红绿灯1");
                ControlDevices.Add("红绿灯2");
                ControlDevices.Add("红外1");
                ControlDevices.Add("红外2");
                ControlDevices.Add("地感1");
                ControlDevices.Add("地感2");
            }

            ///设置控制器端子
            if(ControlPorts.Count == 0)
            {
                for (int i = 0; i < 8; i++)
                {
                    ControlPorts.Add(i+1);
                }
            }

            Provices.Add(new Province() { Count = 0, Name = "豫" });
            Provices.Add(new Province() { Count = 0, Name = "京" });
            Provices.Add(new Province() { Count = 0, Name = "津" });
            Provices.Add(new Province() { Count = 0, Name = "沪" });
            Provices.Add(new Province() { Count = 0, Name = "渝" });
            Provices.Add(new Province() { Count = 0, Name = "冀" });
            Provices.Add(new Province() { Count = 0, Name = "云" });
            Provices.Add(new Province() { Count = 0, Name = "辽" });
            Provices.Add(new Province() { Count = 0, Name = "黑" });
            Provices.Add(new Province() { Count = 0, Name = "湘" });
            Provices.Add(new Province() { Count = 0, Name = "皖" });
            Provices.Add(new Province() { Count = 0, Name = "鲁" });
            Provices.Add(new Province() { Count = 0, Name = "新" });
            Provices.Add(new Province() { Count = 0, Name = "苏" });
            Provices.Add(new Province() { Count = 0, Name = "浙" });
            Provices.Add(new Province() { Count = 0, Name = "赣" });
            Provices.Add(new Province() { Count = 0, Name = "鄂" });
            Provices.Add(new Province() { Count = 0, Name = "桂" });
            Provices.Add(new Province() { Count = 0, Name = "甘" });
            Provices.Add(new Province() { Count = 0, Name = "晋" });
            Provices.Add(new Province() { Count = 0, Name = "蒙" });
            Provices.Add(new Province() { Count = 0, Name = "陕" });
            Provices.Add(new Province() { Count = 0, Name = "吉" });
            Provices.Add(new Province() { Count = 0, Name = "闽" });
            Provices.Add(new Province() { Count = 0, Name = "贵" });
            Provices.Add(new Province() { Count = 0, Name = "粤" });
            Provices.Add(new Province() { Count = 0, Name = "青" });
            Provices.Add(new Province() { Count = 0, Name = "藏" });
            Provices.Add(new Province() { Count = 0, Name = "川" });
            Provices.Add(new Province() { Count = 0, Name = "宁" });
            Provices.Add(new Province() { Count = 0, Name = "琼" });


            Letters.Add(new Letter { Count =0,Name="A"});
            Letters.Add(new Letter { Count = 0, Name = "B" });
            Letters.Add(new Letter { Count = 0, Name = "C" });
            Letters.Add(new Letter { Count = 0, Name = "D" });
            Letters.Add(new Letter { Count = 0, Name = "E" });
            Letters.Add(new Letter { Count = 0, Name = "F" });
            Letters.Add(new Letter { Count = 0, Name = "G" });
            Letters.Add(new Letter { Count = 0, Name = "H" });
            Letters.Add(new Letter { Count = 0, Name = "I" });
            Letters.Add(new Letter { Count = 0, Name = "J" });
            Letters.Add(new Letter { Count = 0, Name = "K" });
            Letters.Add(new Letter { Count = 0, Name = "L" });
            Letters.Add(new Letter { Count = 0, Name = "M" });
            Letters.Add(new Letter { Count = 0, Name = "N" });
            Letters.Add(new Letter { Count = 0, Name = "O" });
            Letters.Add(new Letter { Count = 0, Name = "P" });
            Letters.Add(new Letter { Count = 0, Name = "Q" });
            Letters.Add(new Letter { Count = 0, Name = "R" });
            Letters.Add(new Letter { Count = 0, Name = "S" });
            Letters.Add(new Letter { Count = 0, Name = "T" });
            Letters.Add(new Letter { Count = 0, Name = "U" });
            Letters.Add(new Letter { Count = 0, Name = "V" });
            Letters.Add(new Letter { Count = 0, Name = "W" });
            Letters.Add(new Letter { Count = 0, Name = "X" });
            Letters.Add(new Letter { Count = 0, Name = "Y" });
            Letters.Add(new Letter { Count = 0, Name = "Z" });

            ///添加默认计算公式
            Expressions.Add(new Expression()
            {
                UseType = "weight",
                Result = "Net",
                Formula = "Gross-Tare",
                IsNegative = false,
                IsRound = false,
                decimals = 3,
                FactorLimits = new List<FactorLimit>()
            });
            Expressions[0].FactorLimits.Add(new FactorLimit {
                Val = 0,
                Limit="大于等于",
                FieldFactor="Gross"
            });
            Expressions[0].FactorLimits.Add(new FactorLimit
            {
                Val = 0,
                Limit = "大于等于",
                FieldFactor = "Tare"
            });
            Relations.Add(new RelationLetter { Code = "=", Alias = "等于" });
            Relations.Add(new RelationLetter { Code = "<>", Alias = "不等于" });
            Relations.Add(new RelationLetter { Code=">" ,Alias="大于"});
            Relations.Add(new RelationLetter { Code = ">=", Alias = "大于等于" });
            Relations.Add(new RelationLetter { Code = "<", Alias = "小于" });
            Relations.Add(new RelationLetter { Code = "<=", Alias = "小于等于" });
            Relations.Add(new RelationLetter { Code = "like", Alias = "包含" });
            Relations.Add(new RelationLetter { Code = " not like", Alias = "不包含" });
            Logicals.Add(new LogicalLetter() { Code="and",Alias="并且" });
            Logicals.Add(new LogicalLetter() { Code = "or", Alias = "或者" });
        }

        #region   词组配置
        /// <summary>
        /// 词组表名和前缀、名称 映射
        /// </summary>
        public class PhraseMap
        {
            public PhraseMap()
            {
                ItemName = "未配置(显示名称)";
                Id = "未配置(表名)";
                Code = "未配置(前缀)";
                Relations = new List<Relation>();
            }
            [XmlAttribute]
            /// <summary>
            /// 名称
            /// </summary>
            public string ItemName { get; set; }

            [XmlAttribute]
            /// <summary>
            /// 表名作为id
            /// </summary>
            public string Id { get; set; }

            [XmlAttribute]
            /// <summary>
            /// 简拼
            /// </summary>
            public string Code { get; set; }

            /// <summary>
            /// 绑定的字段
            /// </summary>
            [XmlAttribute]
            public string BindField { get; set; }

            /// <summary>
            /// 映射关系
            /// </summary>
            public List<Relation> Relations { get; set; }


            public class Relation
            {
                /// <summary>
                /// 关联词组的编码
                /// </summary>
                [XmlAttribute]
                public string RelationCode { get; set; }

                /// <summary>
                /// 关联保存的字段
                /// </summary>
                [XmlAttribute]
                public string Field { get; set; }

                /// <summary>
                /// 关联字段名称
                /// </summary>
                [XmlText]
                public string FieldName { get; set; }
            }
        }

        #endregion 词组配置

        #region 台号
        /// <summary>
        /// 台号
        /// </summary>
        public class ScaleNum
        {
            [XmlAttribute]
            public string Code { get; set; }

            [XmlText]
            public string Name { get; set; }
        }

        #endregion 台号

        #region 仪表类型
        /// <summary>
        /// 仪表类型
        /// </summary>
        public class MeterType
        {
           /// <summary>
           /// 仪表类型名称
           /// </summary>
           [XmlText]
            public string Name { get; set; }

            /// <summary>
            /// 类型编号
            /// </summary>
            [XmlAttribute]
            public string Code { get; set; }

        }

        #endregion 仪表类型

        #region 计量单位
        /// <summary>
        /// 计量单位集合实体类
        /// </summary>
        public  class WeightUnit
        {
            /// <summary>
            /// 名称
            /// </summary>
            [XmlText]
            public string Name { get; set; }

            /// <summary>
            /// 编号
            /// </summary>
            [XmlAttribute]
            public string Code { get; set; }
        }

        #endregion 计量单位

        #region 车号词组
        /// <summary>
        /// 省份和 直辖市简称
        /// </summary>
        [Serializable]
        public class Province
        {
            [XmlAttribute]
            public int Count { get; set; }

            [XmlText]
            public string Name { get; set; }
        }

        /// <summary>
        /// 26个英文字母
        /// </summary>
        [Serializable]
        public class Letter
        {
            [XmlAttribute]
            public int Count { get; set; }

            [XmlText]
            public string Name { get; set; }
        }

        #endregion 车号词组

        #region 计算公式
       /// <summary>
       /// 计算公式实体类
       /// </summary>
        public class Expression
        {
            [XmlAttribute]
            /// <summary>
            /// 使用类别（weight 代表称重使用）
            /// </summary>
            public string  UseType { get; set; }
            [XmlAttribute]
            /// <summary>
            /// 计算结果字段
            /// </summary>
            public string Result { get; set; }

            private string formula;
            [XmlAttribute]
            /// <summary>
            /// 计算公式
            /// </summary>
            public string Formula {
                get {
                    return formula;
                }
                set{
                    formula = value;
                    if(!string.IsNullOrEmpty(formula))
                    {
                        string[] arr = formula.Split(new char[] {'(',')','+','-','*','/' },StringSplitOptions.RemoveEmptyEntries);
                        Factor = new List<string>();
                        Factor.AddRange(arr);
                    }
                }
            }

            /// <summary>
            /// 因子
            /// </summary>
            [XmlIgnore]
            public List<string> Factor { get; set; }

            /// <summary>
            /// 因子限制
            /// </summary>
            public List<FactorLimit> FactorLimits { get; set; }

            /// <summary>
            ///是否四舍五入
            /// </summary>
            [XmlAttribute]
            public bool IsRound { get; set; }

            /// <summary>
            /// 保留的小数位
            /// </summary>
            [XmlAttribute]
            public int decimals { get; set; }

            /// <summary>
            /// 是否允许为负数
            /// </summary>
            [XmlAttribute]
            public bool IsNegative { get; set; }
        }

        /// <summary>
        /// 因子限制
        /// </summary>
        public class FactorLimit
        {
            /// <summary>
            /// 参考值
            /// </summary>
            [XmlAttribute]
            public double Val { get; set; }
            /// <summary>
            /// 限制条件
            /// </summary>
            [XmlAttribute]
            public string Limit { get; set; }
            /// <summary>
            /// 限制因子
            /// </summary>
            [XmlAttribute]
            public string FieldFactor { get; set; }
        }
        #endregion 计算公式

        #region 比较关系
        /// <summary>
        /// 比较关系
        /// </summary>
        public class RelationLetter
        {
            [XmlAttribute]
            /// <summary>
            /// 编码
            /// </summary>
            public string Code { get; set; }

            [XmlText]
            /// <summary>
            /// 别名
            /// </summary>
            public string Alias { get; set; }
        }
        #endregion 比较关系

        #region 逻辑关系

        /// <summary>
        /// 逻辑关系
        /// </summary>
        public class LogicalLetter
        {
            [XmlAttribute]
            /// <summary>
            /// 编码
            /// </summary>
            public string Code { get; set; }

            [XmlText]
            /// <summary>
            /// 别名
            /// </summary>
            public string Alias { get; set; }
        }

        #endregion 逻辑关系
    }
}
